package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.QueryAchievementTeamMemberDetailReq;
import com.sg.dto.base.app.req.QueryAchievementTeamMemberListReq;
import com.sg.entity.OmsAchievementTeamMember;
import com.sg.service.base.app.MOmsAchievementTeamMemberService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsAchievementTeamMemberMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAchievementTeamMemberServiceImpl
        extends ServiceImpl<OmsAchievementTeamMemberMapper, OmsAchievementTeamMember>
        implements MOmsAchievementTeamMemberService {

    @Autowired
    OmsAchievementTeamMemberMapper omsAchievementTeamMemberMapper;

    /**
     * 3-10-06新增成果团队成员[9125]
     * gen by moon at 4/3/2024, 11:08:11 PM
     */
    @Trace(operationName = "3-10-06新增成果团队成员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAchievementTeamMember(OmsAchievementTeamMember omsAchievementTeamMember) {
        omsAchievementTeamMember.setAchievementTeamMemberId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAchievementTeamMember.setIsArchive("FALSE");
        omsAchievementTeamMember.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAchievementTeamMember.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementTeamMember != null && omsAchievementTeamMember.getSubjectLifeCycle() != null && omsAchievementTeamMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTeamMember.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAchievementTeamMember.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAchievementTeamMember.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementTeamMember != null && omsAchievementTeamMember.getSubjectLifeCycle() != null && omsAchievementTeamMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTeamMember.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAchievementTeamMember.setIsValid("FALSE");
        if (super.save(omsAchievementTeamMember)) {
            return omsAchievementTeamMember.getAchievementTeamMemberId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-10-06修改成果团队成员[9126]
     * gen by moon at 4/3/2024, 11:08:12 PM
     */
    @Trace(operationName = "3-10-06修改成果团队成员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAchievementTeamMember(OmsAchievementTeamMember omsAchievementTeamMember) {
        omsAchievementTeamMember.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementTeamMember != null && omsAchievementTeamMember.getSubjectLifeCycle() != null && omsAchievementTeamMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTeamMember.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAchievementTeamMember != null && omsAchievementTeamMember.getSubjectLifeCycle() != null && omsAchievementTeamMember.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementTeamMember.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAchievementTeamMember.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementTeamMember != null && omsAchievementTeamMember.getSubjectLifeCycle() != null && omsAchievementTeamMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTeamMember.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAchievementTeamMember != null && omsAchievementTeamMember.getSubjectLifeCycle() != null && omsAchievementTeamMember.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementTeamMember.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAchievementTeamMember);
    }

    /**
     * 3-10-06查询成果团队成员列表[9127]
     * gen by moon at 4/3/2024, 11:08:15 PM
     */
    @Trace(operationName = "3-10-06查询成果团队成员列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementTeamMember> queryAchievementTeamMemberList(QueryAchievementTeamMemberListReq reqDto) {

        try {
            QueryWrapper<OmsAchievementTeamMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementTeamMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementTeamMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementTeamMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAchievementTeamMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-06删除成果团队成员[9128]
     * gen by moon at 4/3/2024, 11:08:16 PM
     */
    @Trace(operationName = "3-10-06删除成果团队成员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAchievementTeamMember(String achievementTeamMemberId) {

        return super.removeById(achievementTeamMemberId);
    }

    /**
     * 3-10-06批量删成果团队成员[9143]
     * gen by moon at 4/3/2024, 11:08:20 PM
     */
    @Trace(operationName = "3-10-06批量删成果团队成员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteAchievementTeamMember(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-10-06查询成果团队成员详情[9227]
     * gen by moon at 4/3/2024, 11:08:23 PM
     */
    @Trace(operationName = "3-10-06查询成果团队成员详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAchievementTeamMember queryAchievementTeamMemberDetail(QueryAchievementTeamMemberDetailReq reqDto) {

        try {

            QueryWrapper<OmsAchievementTeamMember> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAchievementTeamMember> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAchievementTeamMember retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-06批量改成果团队成员[9229]
     * gen by moon at 4/4/2024, 10:03:10 AM
     */
    @Trace(operationName = "3-10-06批量改成果团队成员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateAchievementTeamMember(List<OmsAchievementTeamMember> omsAchievementTeamMember) {

        if (omsAchievementTeamMember == null || omsAchievementTeamMember.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsAchievementTeamMember);

    }
}
